package demo;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
//import okhttp3.Headers;
//import okhttp3.OkHttpClient;
//import okhttp3.Request;
//import okhttp3.Response;
//import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
//import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
//import org.apache.hc.client5.http.impl.classic.HttpClients;
//import org.apache.hc.core5.http.ClassicHttpRequest;
//import org.apache.hc.core5.http.HttpEntity;
//import org.apache.hc.core5.http.io.entity.EntityUtils;
//import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
//import org.apache.hc.core5.http.message.BasicNameValuePair;
import cn.hutool.json.JSONUtil;
import org.junit.jupiter.api.Test;

//import java.io.BufferedOutputStream;
//import java.io.IOException;
//import java.util.Arrays;
import java.util.Map;

public class HTTPFileDemo {
//    OkHttpClient client = new OkHttpClient();

    String url = "https://cdn.azul.com/zulu/bin/zulu21.32.17-ca-jdk21.0.2-win_x64.zip";


    @Test
    void hutool() {
        HttpRequest request = HttpUtil.createGet(url);
//        HttpRequest request = HttpUtil.createGet("https://www.azul.com/core-post-download/?endpoint=zulu&uuid=086da5b6-c1b8-49c6-8369-fc62bf7f504c");
        HttpResponse response = request.execute();
        Map<String, java.util.List<String>> headers = response.headers();
        System.out.println(headers);
        System.out.println(response.getFileNameFromDisposition(null));
        response.writeBody("build/zuluJdk.zip");

    }

//    @Test
//    void okhttp() throws IOException {
////        OkHttpClient
//        Request request = new Request.Builder()
//                .url(url)
//                .build();
//
//        try (Response response = client.newCall(request).execute()) {
//            Headers headers = response.headers();
//
//            System.out.println(headers);
////            response.body().bytes();
//        }
//    }
//
//    @Test
//    void httpClient() throws IOException {
//        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
//            ClassicHttpRequest httpGet = ClassicRequestBuilder.get("http://httpbin.org/get")
//                    .build();
//            // The underlying HTTP connection is still held by the response object
//            // to allow the response content to be streamed directly from the network socket.
//            // In order to ensure correct deallocation of system resources
//            // the user MUST call CloseableHttpResponse#close() from a finally clause.
//            // Please note that if response content is not fully consumed the underlying
//            // connection cannot be safely re-used and will be shut down and discarded
//            // by the connection manager.
//            httpclient.execute(httpGet, response -> {
//                System.out.println(response.getCode() + " " + response.getReasonPhrase());
//                final HttpEntity entity1 = response.getEntity();
//                // do something useful with the response body
//                // and ensure it is fully consumed
//                EntityUtils.consume(entity1);
//                return null;
//            });
//
//            ClassicHttpRequest httpPost = ClassicRequestBuilder.post("http://httpbin.org/post")
//                    .setEntity(new UrlEncodedFormEntity(Arrays.asList(
//                            new BasicNameValuePair("username", "vip"),
//                            new BasicNameValuePair("password", "secret"))))
//                    .build();
//            httpclient.execute(httpPost, response -> {
//                System.out.println(response.getCode() + " " + response.getReasonPhrase());
//                final HttpEntity entity2 = response.getEntity();
//                // do something useful with the response body
//                // and ensure it is fully consumed
//                EntityUtils.consume(entity2);
//                return null;
//            });
//        }
//    }
}
